package com.platform.toutiao.config;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;

public class TouTiaoConfig {
	protected volatile String appId;
	protected volatile String secret;
  
	protected volatile String accessToken;
	protected volatile long expiresTime;
	protected volatile ApacheHttpClientBuilder apacheHttpClientBuilder;

  
  
	protected volatile String httpProxyHost;
	protected volatile int httpProxyPort;
	protected volatile String httpProxyUsername;
	protected volatile String httpProxyPassword;

	protected Lock accessTokenLock = new ReentrantLock();

  /**
   * a应该是线程安全的.
   *
   * @param accessToken      新的accessToken值
   * @param expiresInSeconds 过期时间，以秒为单位
   */
	public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
		this.accessToken = accessToken;
		this.expiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L;
	}
  
  /**
   * a强制将ticket过期掉.
   */
	public void expireAccessToken() {
		this.expiresTime = 0;
	}
	public boolean isAccessTokenExpired() {
	    return System.currentTimeMillis() > this.expiresTime;
	}
	
	
	
	public String getAppId() {
		return appId;
	}

	public void setAppId(String appId) {
		this.appId = appId;
	}

	public String getSecret() {
		return secret;
	}

	public void setSecret(String secret) {
		this.secret = secret;
	}

	public String getAccessToken() {
		return accessToken;
	}

	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}

	public long getExpiresTime() {
		return expiresTime;
	}

	public void setExpiresTime(long expiresTime) {
		this.expiresTime = expiresTime;
	}

	public ApacheHttpClientBuilder getApacheHttpClientBuilder() {
		return apacheHttpClientBuilder;
	}

	public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) {
		this.apacheHttpClientBuilder = apacheHttpClientBuilder;
	}

	public String getHttpProxyHost() {
		return httpProxyHost;
	}

	public void setHttpProxyHost(String httpProxyHost) {
		this.httpProxyHost = httpProxyHost;
	}

	public int getHttpProxyPort() {
		return httpProxyPort;
	}

	public void setHttpProxyPort(int httpProxyPort) {
		this.httpProxyPort = httpProxyPort;
	}

	public String getHttpProxyUsername() {
		return httpProxyUsername;
	}

	public void setHttpProxyUsername(String httpProxyUsername) {
		this.httpProxyUsername = httpProxyUsername;
	}

	public String getHttpProxyPassword() {
		return httpProxyPassword;
	}

	public void setHttpProxyPassword(String httpProxyPassword) {
		this.httpProxyPassword = httpProxyPassword;
	}

	public Lock getAccessTokenLock() {
		return accessTokenLock;
	}

	public void setAccessTokenLock(Lock accessTokenLock) {
		this.accessTokenLock = accessTokenLock;
	}
	
}
